package com.suixun.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.suixun.dao.SchoolinfoMapper;
import com.suixun.dao.attendancemachine.AttendanceMachineDao;
import com.suixun.dao.attendancemachine.AttendanceMachineMapper;
import com.suixun.dao.attendancemachine.LogAttendanceMachineDao;
import com.suixun.entity.Schoolinfo;
import com.suixun.entity.SchoolinfoExample;
import com.suixun.entity.attendancemachine.AttendanceMachine;
import com.suixun.entity.attendancemachine.AttendanceMachineExample;
import com.suixun.entity.attendancemachine.ExtAttendanceMachine;
import com.suixun.entity.attendancemachine.LogAttendanceMachine;
import com.suixun.entity.page.PageModel;
import com.suixun.service.AttendanceMachineService;
import com.suixun.util.StringUtil;

import rop.thirdparty.org.apache.commons.lang3.StringUtils;

@Service
public class AttendanceMachineServiceImpl implements AttendanceMachineService {

	@Resource
	private AttendanceMachineMapper attendanceMachineMapper;
	@Resource
	private SchoolinfoMapper schoolinfoMapper;
	@Resource
	private AttendanceMachineDao attendanceMachineDao;
	@Resource
	private LogAttendanceMachineDao logAttendanceMachineDao;

	/**
	 * <p>
	 * Title: addAttendanceMachine
	 * </p>
	 * <p>
	 * 功能描述: 新增考勤机
	 * </p>
	 * 
	 * @param schoolName
	 * @param factoryCode
	 * @param deviceCode
	 * @param name
	 * @return
	 */
	@Override
	public String addAttendanceMachine(String schoolName, String factoryCode, String deviceCode, String name) {
		if (StringUtils.isBlank(schoolName)) {
			return "学校名称为空";
		}
		if (StringUtils.isBlank(deviceCode)) {
			return "考勤机编号为空";
		}
		if (StringUtils.isBlank(factoryCode)) {
			return "生产商编号为空";
		}
		Schoolinfo schoolinfo = getSchoolBySchoolName(schoolName);
		if (schoolinfo == null) {
			return "学校不存在";
		}
		AttendanceMachine attendanceMachine = getAttendanceMachineByDeviceCode(deviceCode);
		if (attendanceMachine != null) {
			return "考勤机编号已存在";
		}
		attendanceMachine = new AttendanceMachine();
		Date date = new Date();
		attendanceMachine.setCreateTime(date);
		attendanceMachine.setDeviceCode(deviceCode);
		attendanceMachine.setFactoryCode(factoryCode);
		attendanceMachine.setName(name);
		attendanceMachine.setStatus(true);
		attendanceMachine.setSchoolid(schoolinfo.getId());
		attendanceMachine.setUpdateTime(date);
		attendanceMachineMapper.insert(attendanceMachine);
		return null;
	}

	private Schoolinfo getSchoolBySchoolName(String schoolName) {
		SchoolinfoExample schoolinfoExample = new SchoolinfoExample();
		schoolinfoExample.createCriteria().andSchoolnameEqualTo(schoolName);
		List<Schoolinfo> schoolinfoList = schoolinfoMapper.selectByExample(schoolinfoExample);
		if (CollectionUtils.isEmpty(schoolinfoList)) {
			return null;
		} else {
			return schoolinfoList.get(0);
		}
	}

	/**
	 * <p>
	 * Title: updateAttendanceMachine
	 * </p>
	 * <p>
	 * 功能描述: 修改考勤机
	 * </p>
	 * 
	 * @param attendanceMachineId
	 * @param schoolName
	 * @param factoryCode
	 * @param deviceCode
	 * @param name
	 * @return
	 */
	@Override
	public String updateAttendanceMachine(Integer attendanceMachineId, String schoolName, String factoryCode,
			String deviceCode, String name, Boolean status) {
		if (attendanceMachineId == null) {
			return "考勤机id为空";
		}
		if (StringUtils.isNotBlank(deviceCode)) {
			AttendanceMachine attendanceMachine = getAttendanceMachineByDeviceCode(deviceCode);
			if (attendanceMachine != null
					&& !String.valueOf(attendanceMachineId).equals(String.valueOf(attendanceMachine.getId()))) {
				return "考勤机编号已存在";
			}
		}
		Integer schoolId = null;
		if (StringUtils.isNotBlank(schoolName)) {
			Schoolinfo schoolinfo = getSchoolBySchoolName(schoolName);
			if (schoolinfo == null) {
				return "学校不存在";
			} else {
				schoolId = schoolinfo.getId();
			}
		}
		AttendanceMachine attendanceMachine = new AttendanceMachine();
		attendanceMachine.setDeviceCode(StringUtils.isBlank(deviceCode) ? null : deviceCode);
		attendanceMachine.setFactoryCode(StringUtils.isBlank(factoryCode) ? null : factoryCode);
		attendanceMachine.setName(StringUtils.isBlank(name) ? null : name);
		attendanceMachine.setSchoolid(schoolId);
		attendanceMachine.setUpdateTime(new Date());
		attendanceMachine.setStatus(status);
		attendanceMachine.setId(attendanceMachineId);
		attendanceMachineMapper.updateByPrimaryKeySelective(attendanceMachine);
		return null;
	}

	private AttendanceMachine getAttendanceMachineByDeviceCode(String deviceCode) {
		AttendanceMachineExample attendanceMachineExample = new AttendanceMachineExample();
		attendanceMachineExample.createCriteria().andDeviceCodeEqualTo(deviceCode);
		List<AttendanceMachine> attendanceMachineList = attendanceMachineMapper
				.selectByExample(attendanceMachineExample);
		if (CollectionUtils.isEmpty(attendanceMachineList)) {
			return null;
		} else {
			return attendanceMachineList.get(0);
		}
	}

	/**
	 * <p>
	 * Title: getAttendanceMachinePage
	 * </p>
	 * <p>
	 * 功能描述: 分页查询考勤机
	 * </p>
	 * 
	 * @param schoolName
	 * @param factoryCode
	 * @param deviceCode
	 * @param pageNumber
	 * @param pageCount
	 * @return
	 */
	@Override
	public PageModel<ExtAttendanceMachine> getAttendanceMachinePage(String schoolName, String factoryCode,
			String deviceCode, Integer pageNumber, Integer pageCount) {
		PageModel<ExtAttendanceMachine> pageModel = new PageModel<>();
		if (StringUtil.isNotEmpty(schoolName)) {
			pageModel.getQueryparam().put("schoolName", schoolName);
		}
		if (StringUtil.isNotEmpty(factoryCode)) {
			pageModel.getQueryparam().put("factoryCode", factoryCode);
		}
		if (StringUtil.isNotEmpty(deviceCode)) {
			pageModel.getQueryparam().put("deviceCode", deviceCode);
		}
		if (pageCount != null) {
			pageModel.setPagesize(pageCount);
		}
		Integer totleRows = attendanceMachineDao.countRows(pageModel);
		pageModel.setTotlepage(totleRows);
		if (pageNumber != null) {
			pageModel.setCurrentpage(pageNumber);
		}
		List<ExtAttendanceMachine> extAttendanceMachineList = attendanceMachineDao.queryByPaging(pageModel);
		pageModel.setQueryresult(extAttendanceMachineList);
		return pageModel;
	}

	/**
	 * <p>
	 * Title: getLogAttendanceMachinePage
	 * </p>
	 * <p>
	 * 功能描述: 分页查询考勤机记录
	 * </p>
	 * 
	 * @param attendanceMachineId
	 * @param sortStatus
	 *            排序：0降序，1升序
	 * @param sortParam
	 *            按参数排序：0上报时间，1上报状态
	 * @param pageNumber
	 * @param pageCount
	 * @return
	 */
	@Override
	public PageModel<LogAttendanceMachine> getLogAttendanceMachinePage(Integer attendanceMachineId, int sortStatus,
			int sortParam, Integer pageNumber, Integer pageCount) {
		PageModel<LogAttendanceMachine> pageModel = new PageModel<>();
		if(attendanceMachineId == null) {
			return pageModel;
		}
		pageModel.getQueryparam().put("attendanceMachineId", attendanceMachineId);
		pageModel.getQueryparam().put("sortStatus", sortStatus);
		pageModel.getQueryparam().put("sortParam", sortParam);
		if (pageCount != null) {
			pageModel.setPagesize(pageCount);
		}
		Integer totleRows = logAttendanceMachineDao.countRows(pageModel);
		pageModel.setTotlepage(totleRows);
		if (pageNumber != null) {
			pageModel.setCurrentpage(pageNumber);
		}
		List<LogAttendanceMachine> logAttendanceMachineList = logAttendanceMachineDao.queryByPaging(pageModel);
		pageModel.setQueryresult(logAttendanceMachineList);
		return pageModel;
	}
}
